package com.caocaog.market.controller;

import com.caocaog.market.entity.PushMessageTemplate;
import com.caocaog.market.service.PushMessageTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.aspect.annotation.Trim;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.base.controller.GenericController;
import org.jeecg.common.system.base.entity.groups.UpdateGroup;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 推送消息模版管理
 *
 * @author chechen
 * @date 2022-05-21
 */
@RestController
@Api(value = "pushMessageTemplate", tags = PushMessageTemplateController.TABLE_MEANING)
@RequestMapping("/api/pushMessageTemplate")
public class PushMessageTemplateController extends GenericController<PushMessageTemplate, PushMessageTemplateService> {

    public static final String TABLE_MEANING = "推送消息模版管理";

    @GetMapping("/list")
    @ApiOperation(value = TABLE_MEANING + LIST, notes = "获取推送消息模版集合（缓存）")
    public Result<List<PushMessageTemplate>> list() {
        return Result.OK(super.service.getPushMessageTemplateList());
    }

    @Trim
    @PostMapping
    @AutoLog(value = TABLE_MEANING + ADD, operateType = CommonConstant.OPERATE_TYPE_2)
    @ApiOperation(value = TABLE_MEANING + ADD, notes = "新增推送消息模版（缓存）")
    public Result<String> add(@RequestBody @Validated PushMessageTemplate pushMessageTemplate) {
        pushMessageTemplate.clearFields();
        pushMessageTemplate.setCreateBy("admin");
        return Result.OK(ADD_SUCCESS, super.service.addPushMessageTemplate(pushMessageTemplate).toString());
    }

    @Trim
    @PutMapping
    @AutoLog(value = TABLE_MEANING + EDIT, operateType = CommonConstant.OPERATE_TYPE_3)
    @ApiOperation(value = TABLE_MEANING + EDIT, notes = "根据id更新推送消息模版（缓存）")
    public Result<Integer> update(@RequestBody @Validated(UpdateGroup.class) PushMessageTemplate pushMessageTemplate) {
        pushMessageTemplate.clearFields();
        pushMessageTemplate.setCreateBy("admin");
//        pushMessageTemplate.setCreateBy(((LoginUser) SecurityUtils.getSubject().getPrincipal()).getUsername());
        return Result.OK(EDIT_SUCCESS, super.service.updatePushMessageTemplate(pushMessageTemplate));
    }

    @DeleteMapping
    @AutoLog(value = TABLE_MEANING + DELETE, operateType = CommonConstant.OPERATE_TYPE_4)
    @ApiOperation(value = TABLE_MEANING + DELETE, notes = "根据id删除推送消息模版（缓存）")
    public Result<Integer> delete(@RequestParam("id") Integer pushMessageTemplateId) {
        return Result.OK(DELETE_SUCCESS, super.service.deletePushMessageTemplate(pushMessageTemplateId));
    }

}
